package com.xiaoqi.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.xiaoqi.constant.MessageConstant;
import com.xiaoqi.exception.SunnydayException;
import com.xiaoqi.pojo.CheckGroup;
import com.xiaoqi.pojo.PageResult;
import com.xiaoqi.pojo.QueryPageBean;
import com.xiaoqi.pojo.Result;
import com.xiaoqi.service.CheckGroupService;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author PC
 * creationTime: 2022/10/18 17:40
 */

@RequestMapping("/checkgroup")
@RestController
public class CheckGroupController {

    @Reference
    private CheckGroupService checkGroupService;

    /**
     * 查询所有检查组
     * @return
     */
    @GetMapping("/findAll")
    public Result findAll(){
        List<CheckGroup> list = checkGroupService.findAll();
        return new Result(true, MessageConstant.QUERY_CHECKGROUP_SUCCESS,list);
    }

    /**
     * 分页查询所有检查组
     * @return
     */
    @GetMapping("/findPage")
    public Result findPage(
            @RequestParam(name = "currentPage",defaultValue = "1") Integer currentPage,
            @RequestParam(name = "pageSize",defaultValue = "10") Integer pageSize,
            @RequestParam(name = "queryString",defaultValue = "") String queryString){
        QueryPageBean queryPageBean = new QueryPageBean(currentPage,pageSize,queryString);
        PageResult pageResult = checkGroupService.findPage(queryPageBean);
        return new Result(true, MessageConstant.QUERY_CHECKGROUP_SUCCESS,pageResult);
    }
    /**
     * 新增检查项
     * @param checkGroup
     */
    @PostMapping("/addCheckGroup")
    public Result addCheckGroup(@RequestBody CheckGroup checkGroup,Integer [] checkitemIds){
        checkGroupService.addCheckGroup(checkGroup,checkitemIds);
        return new Result(true, MessageConstant.ADD_CHECKGROUP_SUCCESS);
    }

    /**
     * 根据条件"id"查询检查组
     * @return
     */
    @GetMapping("/findById")
    public Result findById(@RequestParam(name = "id") Integer id){
        CheckGroup checkGroup = checkGroupService.findById(id);
        return new Result(true, MessageConstant.QUERY_CHECKGROUP_SUCCESS,checkGroup);
    }

    /**
     * 根据条件检查组"id"查询检查组关联的检查项ids
     * @return
     */
    @GetMapping("/findCheckGroup_CheckItemById")
    public Result findCheckGroup_CheckItemById(@RequestParam(name = "id") Integer id){
        List<Integer> checkItemIds = checkGroupService.findCheckGroup_CheckItemById(id);
        return new Result(true, MessageConstant.QUERY_CHECKITEM_SUCCESS,checkItemIds);
    }

    /**
     * 根据条件检查组"id"修改检查组信息
     * @return
     */
    @PutMapping("/update")
    public Result update(@RequestBody CheckGroup checkGroup,Integer[] checkitemIds){
        checkGroupService.update(checkGroup,checkitemIds);
        return new Result(true, MessageConstant.EDIT_CHECKGROUP_SUCCESS);
    }


    /**
     * 根据条件检查组"id"删除检查组
     * @return
     */
    @DeleteMapping("/del")
    public Result del(@RequestParam(name = "id") Integer id){
        checkGroupService.delCheckGroup(id);
        return new Result(true, MessageConstant.DELETE_CHECKGROUP_SUCCESS);
    }

    @RequestMapping("/err")
    public String err(){

        if (1>0){

            throw new SunnydayException("自定义错误");
        }
        return "!@3";
    }
}
